import Vue from 'vue'
import Vuex from 'vuex'
import http from '../utils/http'

Vue.use(Vuex)
//购物车vuex信息
var carts = {
  namespaced: true,
  state: {
    cartList: []
  },
  getters: {
    getPay(state){
      let temp =state.cartList.filter(item=>{
        return item.checked==true
      })
      return temp
    }
  },
  mutations: {
    updateList(state, value) {
      value.forEach(item=>{
        item.checked=false
      })
      state.cartList = value
      /* console.log(111111,state.cartList); */
    },
    addList(state, value) {
      state.cartList.push(value)
    }
  },
  actions: {
    getList(context) {
      let uid = JSON.parse(localStorage.getItem('user')).uid
      http.get('/api/cartlist', { params: { uid } }).then(res => {
        context.commit('updateList',res.list)
        /* console.log(res.list); */
      })
    },
    addShop(context, value) {
      http.get("/api/goodsinfo", { params: { id: value.id } }).then((res) => {
        res.list.img = value.img + res.list.img
        /* console.log(res.list);
        console.log(context.state.cartList.length); */
          context.commit('addList', res.list)
      })
    },
    /* seeSure(context, value) {
      return context.state.cartList.some(item => {
        return item.id == value
      })
    }, */
  },
}
//用户vuex信息
var user = {
  namespaced: true,
  state: {
  },
  getters: {
  },
  mutations: {
  },
  actions: {
  },
}

export default new Vuex.Store({
  modules: {
    carts,
    user
  }
})
